<?php


class importexport_ctl_admin_qifa3import extends importexport_controller{

    const TTL = 600;//缓存时间
    const REDIS_DB = 1;
    /*
     *后台显示队列
     * */
    public function queue_import(){
        $params = array(
            'title'=>'导入任务队列',
            'use_buildin_recycle'=>true,
            'orderBy' => 'create_date desc',
            'base_filter'=>array('type'=>'import'),
        );
        $this->finder('importexport_mdl_task',$params);
    }

    /*
     * 导入页面
     * */
    public function import_view(){
        $this->pagedata['check_policy'] = $this->check_policy();
        $this->pagedata['params'] = $_GET['_params'];
        //支持导出类型
        $this->pagedata['import_type'] = $this->import_support_filetype();

        $this->display('admin/import/qifa3import.html');
    }

    /*
     * 导入数据
     * */
    public function create_import(){
        #检查导入文件是否合法

        $this->check_import_file();
        #将导入文件上传到服务器
        $data = $this->push_file($_POST);

        $queue_params = array(
            'model'=>$_POST['mdl'],
            'filetype' => $data['filetype'],
            'policy' => $this->queue_policy(),
            'key'=> $data['key'],
            'app' => $data['app'],
        );

        system_queue::instance()->publish('importexport_tasks_runqifa3import', 'importexport_tasks_runqifa3import', $queue_params);
        $task = app::get('importexport')->model('task')->create_task('import',$data);


        if($task) {
            $echoMsg = app::get('desktop')->_('上传成功,已加入队列');
            $this->import_message(true, $echoMsg);
        }else{
            $echoMsg = app::get('desktop')->_('加入队列失败');
            $this->import_message(true, $echoMsg);
        }
    }





    /**
     * 检查导入文件是否合法
     */
    private function check_import_file(){
        if( !$_FILES['import_file']['name'] ){
            $echoMsg =app::get('importexport')->_('未上传文件');
            $this->import_message(false,$echoMsg);
        }
        $filetype = strrchr($_FILES['import_file']['name'],'.');
        if( '.'.$_POST['filetype'] != $filetype )
        {
            $echoMsg =app::get('importexport')->_('请上传当前选中的文件类型');
            $this->import_message(false,$echoMsg);
        }
        $import_support_filetype = $this->import_support_filetype();
        if(!in_array($filetype,$import_support_filetype)){
            $echoMsg =app::get('importexport')->_('导入格式不支持');
            $this->import_message(false,$echoMsg);
        }
    }



    /**
     * 将导入文件上传到服务器
     * @param array $data
     */
    private function push_file($params){

        $redis = kernel::single('base_kvstore_redis2');
        $redis->selects(self::REDIS_DB);

        $filetype = substr(strrchr($_FILES['import_file']['name'],'.'),1);

        $file = new SplFileObject($_FILES['import_file']['tmp_name']);
        $file->setFlags(SplFileObject::DROP_NEW_LINE);
        $file->setFlags(SplFileObject::READ_CSV);

        $key = $this->gen_key('import:');

        foreach ($file as $k => $row) {
            $redis->lPush($key,serialize($row));
        }
        $params = array(
            'key'=>$key,
            'filetype'=>$filetype,
            'name'=>'导入:'.$_FILES['import_file']['name'],
            'status' => 0,
            'app' => $params['app'],
            'mdl' => $params['mdl'],
            'filetype' => $params['filetype']
        );

        return $params;
    }

}
